package com.ld.hpfs.view.excel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class ErrorDetailExcelView extends AbstractExcelView{

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ArrayList<HashMap<String,Object>> table = (ArrayList<HashMap<String,Object>>) model.get("table");
		HSSFSheet sheet = workbook.createSheet();
		
		String titles[] = { "错误发生时间", "错误类型", "错误表述", "错误科目","相关分析代码维度","相关分析代码"};
		
	    HSSFCellStyle cellStyle = workbook.createCellStyle(); 
	      
	        HSSFRow row = sheet.createRow(0);
	        HSSFCellStyle items_style = workbook.createCellStyle();
	        items_style.setAlignment((short) HSSFCellStyle.ALIGN_CENTER);

	        HSSFFont celltbnamefont = workbook.createFont();
	        celltbnamefont.setFontHeightInPoints((short) 10);

	        celltbnamefont.setColor((short) (HSSFFont.COLOR_RED));
	        items_style.setFont(celltbnamefont);
	        items_style.setWrapText(true);

	        for (int i = 0; i < titles.length; i++) {
	            HSSFCell cell = row.createCell(i);
	            sheet.setColumnWidth(i, 5335);
	            cell.setCellValue(titles[i]);
	            cell.setCellStyle(items_style);
	        }

	        HSSFCellStyle datestyle = workbook.createCellStyle();
	        HSSFDataFormat df = workbook.createDataFormat();
	        datestyle.setDataFormat(df.getFormat("yyyy-mm-dd"));

	        int rowNum = 1;
	        for (HashMap<String,Object> entry : table) {
	        	
	            HSSFRow datarow = sheet.createRow(rowNum++);
	            datarow.createCell(0).setCellStyle(cellStyle);
	            datarow.createCell(1).setCellStyle(cellStyle);
	            datarow.createCell(2).setCellStyle(cellStyle);
	            datarow.createCell(3).setCellStyle(cellStyle);
	            datarow.createCell(4).setCellStyle(cellStyle);
	            if(entry.get("error_data")!=null){
	            	datarow.createCell(0).setCellValue(entry.get("error_data").toString());
	            }
	            if(entry.get("error_type")!=null){
	            	datarow.createCell(1).setCellValue(entry.get("error_type").toString());
	            }
	            if(entry.get("error_desc")!=null){
	            	datarow.createCell(2).setCellValue(entry.get("error_desc").toString());
	            }
	            if(entry.get("param_account")!=null){
	            	datarow.createCell(3).setCellValue(entry.get("param_account").toString());
	            }
	            if(entry.get("tcode")!=null){
	            	datarow.createCell(4).setCellValue(entry.get("tcode").toString());
	            }
	            if(entry.get("stcode")!=null){
	            	datarow.createCell(5).setCellValue(entry.get("stcode").toString());
	            }
	        }
	        
	        
	}

}
